package LeetCode;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName interview
 * @Description: TODO
 * @date ${DAT}21:22
 */
public class LeetCode112 {

    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
    }

    public boolean hasPathSum(TreeNode root, int sum) {
        if (root == null){
            return false;
        }


        //防止根节点就是sum的问题 就是只有一个根节点的情况
       if (root.left == null && root.right == null){
           return root.val == sum;
       }
       if (hasPathSum(root.left,sum - root.val)){
           return true;
       }
       if (hasPathSum(root.right,sum - root.val)){
           return true;
       }
       return false;
    }
}
